package com.zhangyu.shadow.repair.delegate.irregular;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.history.HistoryDelegate;
import com.zhangyu.shadow.repair.delegate.irregular.search.SearchDelegate;
import com.zhangyu.shadow.repair.intercept.DataIntercept;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;
import st.lib.base.delegate.RxShadowDelegate;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.progress.sweetalert.DialogLoader;

/**
 * @author zhangyu
 * @date 2018/2/11
 */

public class IrregularBusDelegate extends RxShadowDelegate {

    public static IrregularBusDelegate create() {
        return new IrregularBusDelegate();
    }

    @BindView(R2.id.toolbar_search)
    Toolbar mToolbar;
    @BindView(R2.id.et_search)
    EditText mEditTextSearch;
    @BindView(R2.id.viewpager)
    ViewPager mViewpager;
    @BindView(R2.id.tabs)
    TabLayout mTabs;
    IrregularPagerAdapter mIrregularPagerAdapter;
    private DialogLoader mDialogLoader;
    private Disposable mDisposable;

    @Override
    public Object setLayout() {
        return R.layout.delegate_irregular_bus;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        if (mDialogLoader == null) {
            mDialogLoader = new DialogLoader(getContext());
        }
        initData();
        requestCount();
    }

    private void initData() {

    }

    private void initView() {
        mEditTextSearch.setHint("请输入车辆编号");
        mEditTextSearch.setInputType(InputType.TYPE_CLASS_NUMBER);
        mEditTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    return true;
                }
                return false;
            }
        });
        mEditTextSearch.setFocusable(false);
        mEditTextSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isFastClick()) {
                    start(SearchDelegate.create());
                }
            }
        });
        initToolbarNav(mToolbar);
    }

    protected void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int i = item.getItemId();
                if (i == R.id.menu_history) {
                    start(HistoryDelegate.create());
                }
                return true;
            }
        });
        toolbar.inflateMenu(R.menu.history);
    }

    private void requestCount() {
        mDisposable = RxRestClient.builder()
                .url(RequestUrls.URL_QUERY_COUNT)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("deptId", AccountManager.getInstance().getDepId())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        ArrayList<BusCountBean> list = new IrregularBusDataConvert()
                                .convertBusCount(response);
                        List<Fragment> fragmentList = new ArrayList<>();
                        fragmentList.add(IrregularTabFragment.newInstance(0));
                        fragmentList.add(IrregularTabFragment.newInstance(1));
                        fragmentList.add(IrregularTabFragment.newInstance(2));
                        mIrregularPagerAdapter = new IrregularPagerAdapter(getChildFragmentManager(),
                                fragmentList, list);
                        mTabs.setupWithViewPager(mViewpager);
                        mViewpager.setOffscreenPageLimit(3);
                        mViewpager.setAdapter(mIrregularPagerAdapter);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                })
                .build()
                .post();
    }

    @Override
    public void onDestroyView() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.onDestroyView();

    }
    @Override
    public boolean onBackPressedSupport() {
        if (!isFastClick()) {
            pop();
        }
        return true;
    }

}
